﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;

namespace BLL
{
    public class StyleBLL
    {
        private readonly DAL.StyleDAL styledal = new DAL.StyleDAL();
        /// <summary>
        /// 根据ID获取StyleName
        /// </summary>
        public string getStyleName(string styleid)
        {
            DataTable dt = styledal.getStyle("ID=" + styleid);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0]["styleName"].ToString();
            }
            else
            {
                return "UnExists";
            }
        }
        /// <summary>
        /// 为该样式购买额外布料
        /// </summary>
        public void optionCloth(string idlist, string orderitemid)
        {
            string[] list = idlist.Split(',');
            for (int i = 0; i < list.Length; i++)
            {
                DataTable dt = styledal.getStyle("ID=" + list[i]);
                if (dt.Rows.Count > 0)
                {
                    if (Boolean.Parse(dt.Rows[0]["buyClothMark"].ToString()))
                    {
                        Albon.Model.ItemCloth itemcloth = new Albon.Model.ItemCloth();
                        ItemClothBLL itemclothbll = new ItemClothBLL();
                        itemcloth.orderItemID = int.Parse(orderitemid);
                        //默认尺寸为一码，如果不是默认尺寸则到OptionClothSize表中查询
                        //不可重复添加，并且已取消的样式要将相应的布料信息也取消
                        itemcloth.clothSize = (decimal)1;
                        itemcloth.styleID = int.Parse(list[i]);
                        itemcloth.Stylename = getTopStyle(list[i]) + "_" + dt.Rows[0]["styleName"].ToString();
                        itemclothbll.addItemCloth(itemcloth);

                    }
                }
            }
        }
        /// <summary>
        /// 根据styleid获取详细的样式信息
        /// </summary>
        /// <param name="p"></param>
        public DataTable getStyleByID(string styleid)
        {
            return styledal.getStyle("ID="+styleid);
        }
        /// <summary>
        /// 根据styleID获取该样式的TopStyle
        /// </summary>
        public string getTopStyle(string styleid)
        {
            DataTable dt = styledal.getStyle("ID=" + styleid);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["parentID"].ToString() != "0")
                {
                    return getTopStyle(dt.Rows[0]["parentID"].ToString());
                }
                else
                {
                    return dt.Rows[0]["styleName"].ToString();
                }
            }
            else
            {
                return "UnExists";
            }
        }
        /// <summary>
        /// 添加样式信息
        /// </summary>
        public int AddStyle(Albon.Model.Style style)
        {
            return styledal.AddStyle(style);
        }
        /// <summary>
        /// 更新样式信息
        /// </summary>
        public void UpdateStyle(Albon.Model.Style style)
        {
            styledal.updateStyle(style);
        }
        /// <summary>
        /// 删除Style,如果该样式下有子样式，则删除所有子样式 
        /// </summary>
        public void DeleteStyle(string styleid)
        {
            DataTable dt = styledal.getStyle("parentID=" + styleid);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DeleteStyle(dr["ID"].ToString());
                }
            }
            string cmdstr = "delete from Style where ID=" + styleid+";delete from StylePrice where styleID="+styleid;
            Commonlib.SqlHelper.ExecuteNonQuery(Commonlib.SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

        /// <summary>
        /// 添加一条样式选择规则 （未定义Model层，只需要传递三个参数）
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <param name="p_3"></param>
        public string addSelectRule(string refID1, string refID2, string rule)
        {
            string ret="";
            if (refID1 == refID2)
            {
                ret = "Can't add a  rule with itself!";
            }
            else
            {
                DataTable dt = styledal.getStyle("ID in (" + refID1 + "," + refID2 + ")");
                string mode1=dt.Rows[0]["selectMode"].ToString();
                string mode2=dt.Rows[1]["selectMode"].ToString();
                string parent1 = dt.Rows[0]["parentID"].ToString();
                string parent2 = dt.Rows[1]["parentID"].ToString();
                if (mode1==mode2)
                {
                    //多选项的规则必须在同一父样式下
                    if (mode1 == "Multi"&&parent1!=parent2)
                    {
                        ret = "Make sure the styles are in the same parentStyle!";
                    }
                    //单选项的规则不可在同一父样式下
                    else if (mode1 == "Single"&&parent1==parent2)
                    {
                        ret = "Can not add a single rule for styles in the same parentStyle!";
                    }
                    //多选项的共存规则不如将两选项合并
                    else if (mode1 == "Multi" && rule == "1")
                    {
                        ret = "Combining the options is better than adding a CoExist rule!";
                    }
                    else
                    {
                        styledal.addSelectRule(refID1, refID2, rule);
                        ret = "Success!";
                    }
                }
                else
                {
                    styledal.addSelectRule(refID1, refID2, rule);
                    ret="Success!";
                }
                
            }
            return ret;
        }

        /// <summary>
        /// 获取样式规则，分别查询refID1和refID2，返回所有匹配的样式
        /// </summary>
        /// <param name="idlist"></param>
        public Hashtable getRule(string idlist)
        {
            Hashtable ht = new Hashtable();
            DataSet ds = styledal.getRule(idlist);
            //合并两个表中的数据
            DataTable dt = ds.Tables[0];
            dt.Merge(ds.Tables[1]);
            if(dt.Rows.Count!=0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (!ht.Contains(dr["parent"].ToString()))
                    {
                        ht[dr["parent"].ToString()] = new Hashtable();
                    }
                    Hashtable htvalue = (Hashtable)ht[dr["parent"].ToString()];
                    if (!htvalue.Contains(dr["rule"].ToString()))
                    {
                        htvalue[dr["rule"].ToString()] = new ArrayList();
                    }
                    if (dr["rule"].ToString() == "1")
                    {
                        ArrayList list = (ArrayList)htvalue["1"];
                        list.Add(dr["refID"].ToString());
                        htvalue.Remove("0");
                    }
                    else
                    {
                        if (htvalue.Contains("1"))
                        {
                            ArrayList list = (ArrayList)htvalue["1"];
                            list.Remove(dr["refID"].ToString());
                        }
                        else
                        {
                            ArrayList list = (ArrayList)htvalue["0"];
                            list.Add(dr["refID"].ToString());
                        }
                    }
                    
                }
            }
            return ht;
        }
        /// <summary>
        /// 获取样式规则
        /// </summary>
        public Hashtable getRule(string keyid, string selectedlist)
        {
            if (selectedlist != "")
            {
                Hashtable ht = getRule(selectedlist);
                ArrayList rule = new ArrayList();
                if (ht.Contains(keyid))
                {
                    return (Hashtable)ht[keyid];
                }
            }
            return null;
            
        }
        /// <summary>
        /// 删除样式规则
        /// </summary>
        public void DeleteRule(string id)
        {
            styledal.deleteRule(id);
        }
    }
}
